from Case.python_ui.Common.basepage import Basepage
import time, pytest
from Case.python_ui.PageLocate.admin_coupon_coupon_template_locator import CouponCouponTemplateLocator
from Case.python_ui.Common.commontool import CommonTool
from selenium.webdriver.common.action_chains import ActionChains
from Case.python_ui.PageLocate.admin_coupon_promo_crowd_locator import AdminCouponPromoCrowdLocator
from Case.python_ui.Pageobject.admin_coupon_promo_crowd_page import AdminCouponPromoCrowdPage
from Case.python_ui.PageLocate.admin_coupon_coupon_records_locator import AdminCouponCouponRecordsLocator
from selenium.common.exceptions import NoSuchElementException
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.PageLocate.user_my_coupon_locator import CouponMyCouponLocator
import datetime


class CouponTemplatePage(Basepage):

    #判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False , '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    #进入Coupon Template 菜单页面
    def enter_coupon_template_page(self):
        time.sleep(2)
        doc = "进入Coupon Template 菜单页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        self.wait_elevisble(CouponCouponTemplateLocator.promo_management_menu, doc)
        self.click_element(CouponCouponTemplateLocator.promo_management_menu, doc)
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_menu, doc)
        self.click_element(CouponCouponTemplateLocator.coupon_template_menu, doc)
        time.sleep(3)

    #创建百分比coupon template
    def create_percent_coupon_template(self):
        time.sleep(2)
        coupon_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建百分比Coupon Template"
        percent = '20'
        self.wait_elevisble(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, coupon_template_name, doc)
        time.sleep(2)
        self.send_keys(CouponCouponTemplateLocator.percent_input, percent, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time_input, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.end_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select_all_selection, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select_all_selection, doc)
        #点击提交
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        #检查创建结果
        self.check_create_coupon_template_result(coupon_template_name)
        #检查coupon template 列表属性
        self.check_list_attribute()
        #检查百分比字段
        doc = '检查百分比字段'
        except_result = percent + '%'
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_search_result_percent, doc).text, except_result)


    #创建固定金额coupon template
    def create_fixed_amount_coupon_template(self):
        time.sleep(2)
        coupon_template_name = 'lwm_auto' + CommonTool.generate_string()
        doc = "创建固定金额Coupon Template"
        fixed_amount = '2000'
        #点击创建按钮
        self.wait_elevisble(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        time.sleep(2)
        #输入名称
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, coupon_template_name, doc)
        time.sleep(2)
        #选择固定金额类型
        self.click_element(CouponCouponTemplateLocator.coupon_type_fixed_amount, doc)
        time.sleep(2)
        #输入固定金额
        self.send_keys(CouponCouponTemplateLocator.fixed_amount_input, fixed_amount, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time_input, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.end_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select_all_selection, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select_all_selection, doc)
        #点击提交
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        #检查创建结果
        self.check_create_coupon_template_result(coupon_template_name)
        #检查固定金额字段
        doc = '检查固定金额字段'
        except_result = '2,000.00'
        self.check_contain_str(self.get_element(CouponCouponTemplateLocator.first_search_result_fixed_amount, doc).text, except_result)

    # 创建重复使用的 coupon template
    def create_reuse_coupon_template(self):
        time.sleep(2)
        coupon_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建重复使用的Coupon Template"
        # 点击创建按钮
        self.wait_elevisble(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        time.sleep(2)
        # 输入template 名称
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, coupon_template_name, doc)
        time.sleep(2)
        # 打开reuse 开关
        self.click_element(CouponCouponTemplateLocator.reuse_button, doc)
        time.sleep(2)
        # 输入百分比
        self.send_keys(CouponCouponTemplateLocator.percent_input, '20', doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time_input, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.end_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select_all_selection, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select_all_selection, doc)
        # 点击提交
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        # 检查创建结果
        self.check_create_coupon_template_result(coupon_template_name)
        #检查reuse字段
        doc = '检查reuse字段'
        except_result = 'Y'
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_search_result_reuse, doc).text, except_result)

    # 创建固定日期coupon template
    def create_fixed_date_coupon_template(self):
        time.sleep(2)
        coupon_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建固定日期Coupon Template"
        percent = '20'
        self.wait_elevisble(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, coupon_template_name, doc)
        time.sleep(2)
        # 输入百分比
        self.send_keys(CouponCouponTemplateLocator.percent_input, percent, doc)
        time.sleep(2)
        # 选择生效时间
        self.click_element(CouponCouponTemplateLocator.start_time_input, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.end_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select_all_selection, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select_all_selection, doc)
        # 点击提交
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        # 检查创建结果
        self.check_create_coupon_template_result(coupon_template_name)

    # 创建动态日期coupon template
    def create_dynamic_coupon_template(self):
        time.sleep(2)
        coupon_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建动态日期Coupon Template"
        percent = '20'
        self.wait_elevisble(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, coupon_template_name, doc)
        time.sleep(2)
        # 输入百分比
        self.send_keys(CouponCouponTemplateLocator.percent_input, percent, doc)
        time.sleep(2)
        # 勾选动态类型
        self.click_element(CouponCouponTemplateLocator.validation_type_dynmic, doc)
        time.sleep(2)

        self.click_element(CouponCouponTemplateLocator.billing_method_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select_all_selection_1, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select_all_selection_1, doc)
        # 点击提交
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        # 检查创建结果
        self.check_create_coupon_template_result(coupon_template_name)
        #检查Validation Period 字段
        doc = '检查Validation Period 字段'
        except_result = '1 Hour after distributed'
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_search_result_validation_period, doc).text, except_result)


    # 创建subscription计费类型的coupon template
    def create_single_billing_method_coupon_template(self):
        time.sleep(2)
        coupon_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建subscription计费类型的Coupon Template"
        percent = '20'
        self.wait_elevisble(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, coupon_template_name, doc)
        time.sleep(2)
        # 输入百分比
        self.send_keys(CouponCouponTemplateLocator.percent_input, percent, doc)
        time.sleep(2)
        # 选择生效时间
        self.click_element(CouponCouponTemplateLocator.start_time_input, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.start_time, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.end_time, doc)
        time.sleep(2)
        # 选择计费类型
        self.click_element(CouponCouponTemplateLocator.billing_method_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select_subscription_selection, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select_all_selection, doc)
        # 点击提交
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        # 检查创建结果
        self.check_create_coupon_template_result(coupon_template_name)
        #检查Billing Method 字段
        doc = '检查Billing Method 字段'
        except_result = 'Subscription'
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_search_result_billing_method, doc).text, except_result)

    # 创建oss产品的coupon template
    def create_single_product_coupon_template(self):
        time.sleep(2)
        coupon_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建slb产品的Coupon Template"
        percent = '20'
        self.wait_elevisble(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.create_coupon_template_button, doc)
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, coupon_template_name, doc)
        time.sleep(2)
        # 输入百分比
        self.send_keys(CouponCouponTemplateLocator.percent_input, percent, doc)
        time.sleep(2)
        # 选择生效时间
        # self.click_element(CouponCouponTemplateLocator.start_time_input, doc)
        # time.sleep(2)
        # self.click_element(CouponCouponTemplateLocator.start_time, doc)
        # time.sleep(2)
        # self.click_element(CouponCouponTemplateLocator.end_time, doc)
        # time.sleep(2)
        # 选择起始时间
        start_time = datetime.datetime.now()
        end_time = start_time + datetime.timedelta(days=5)
        self.send_keys(CouponCouponTemplateLocator.start_time_input, start_time.strftime("%d-%m-%Y"), doc)
        time.sleep(2)
        self.send_keys(CouponCouponTemplateLocator.end_time_input, end_time.strftime("%d-%m-%Y"), doc)
        time.sleep(2)
        #enter 确认
        self.keyboard_event_enter(CouponCouponTemplateLocator.end_time_input, doc)
        time.sleep(2)

        # 选择计费类型
        self.click_element(CouponCouponTemplateLocator.billing_method_select, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.billing_method_select_all_selection, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.usable_product_select, doc)
        time.sleep(2)
        # 选择下拉框的第一个产品
        self.click_element(CouponCouponTemplateLocator.usable_product_select_first_selection, doc)
        product_name = self.get_element(CouponCouponTemplateLocator.usable_product_select_first_selection, doc).text
        # 点击提交
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        # 检查创建结果
        self.check_create_coupon_template_result(coupon_template_name)
        #检查Billing Method 字段
        doc = '检查Billing Method 字段'
        except_result = product_name
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_search_result_usable_product, doc).text, except_result)

    # 创建成功后，根据名称查询，校验列是否展示刚创建的template
    def check_create_coupon_template_result(self, coupon_template_name):
        doc = '验证创建coupon template是否成功'
        time.sleep(2)
        self.wait_elevisble(CouponCouponTemplateLocator.search_input, doc)
        self.send_keys(CouponCouponTemplateLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(5)
        assert self.get_element(CouponCouponTemplateLocator.first_search_result, doc).text == coupon_template_name

    #校验列表元素
    #时区，货币单位需要单独提出来，根据配置文件来校验  --待优化
    def check_list_attribute(self):
        time.sleep(2)
        doc = "校验列表元素"
        coupon_tempalte_list = ['Coupon Template','Status','Billing Method','Usable Product','Percent','Fixed Amount','Reuse','Validation Period','Generator','Generation Time']
        coupon_tempalte_list_coupon_template = self.get_element(CouponCouponTemplateLocator.coupon_template_list_coupon_template, doc).text
        self.check_contain_str(coupon_tempalte_list_coupon_template, coupon_tempalte_list[0])
        coupon_tempalte_list_status = self.get_element(CouponCouponTemplateLocator.coupon_template_list_status, doc).text
        self.check_contain_str(coupon_tempalte_list_status, coupon_tempalte_list[1])
        coupon_tempalte_list_billing_method = self.get_element(CouponCouponTemplateLocator.coupon_template_list_billing_method, doc).text
        self.check_contain_str(coupon_tempalte_list_billing_method, coupon_tempalte_list[2])
        coupon_tempalte_list_usable_product = self.get_element(CouponCouponTemplateLocator.coupon_template_list_usable_product, doc).text
        self.check_contain_str(coupon_tempalte_list_usable_product, coupon_tempalte_list[3])
        coupon_tempalte_list_percent = self.get_element(CouponCouponTemplateLocator.coupon_template_list_percent, doc).text
        self.check_contain_str(coupon_tempalte_list_percent, coupon_tempalte_list[4])
        coupon_tempalte_list_fixed_amount = self.get_element(CouponCouponTemplateLocator.coupon_template_list_fixed_amount, doc).text
        self.check_contain_str(coupon_tempalte_list_fixed_amount, coupon_tempalte_list[5])
        coupon_template_list_reuse = self.get_element(CouponCouponTemplateLocator.coupon_template_list_reuse, doc).text
        self.check_contain_str(coupon_template_list_reuse, coupon_tempalte_list[6])
        coupon_template_list_validation_period = self.get_element(CouponCouponTemplateLocator.coupon_template_list_validation_period, doc).text
        self.check_contain_str(coupon_template_list_validation_period, coupon_tempalte_list[7])
        coupon_template_list_generator = self.get_element(CouponCouponTemplateLocator.coupon_template_list_generator, doc).text
        self.check_contain_str(coupon_template_list_generator, coupon_tempalte_list[8])
        coupon_template_list_generation_time = self.get_element(CouponCouponTemplateLocator.coupon_template_list_generation_time, doc).text
        self.check_contain_str(coupon_template_list_generation_time, coupon_tempalte_list[9])
        time.sleep(3)

    #搜索coupon template
    def query_coupon_template(self):
        time.sleep(2)
        doc = '搜索coupon template'
        fuzzy_query = ['lwm', 'auto']
        #先创建模板，保证有搜索结果
        self.create_fixed_amount_coupon_template()
        time.sleep(2)
        # 模糊查询
        self.wait_elevisble(CouponCouponTemplateLocator.search_input, doc)
        # 清空搜索框
        self.keyboard_event(CouponCouponTemplateLocator.search_input, doc)
        self.send_keys(CouponCouponTemplateLocator.search_input, fuzzy_query[0], doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(5)
        #检查是否能模糊查询
        self.check_contain_str(self.get_element(CouponCouponTemplateLocator.first_search_result, doc).text, fuzzy_query[0])
        # 清空搜索框
        self.keyboard_event(CouponCouponTemplateLocator.search_input, doc)
        # 模糊查询
        self.send_keys(CouponCouponTemplateLocator.search_input, fuzzy_query[1], doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(5)
        # 检查是否能模糊查询
        self.check_contain_str(self.get_element(CouponCouponTemplateLocator.first_search_result, doc).text, fuzzy_query[1])

    #编辑coupon template
    def edit_coupon_template(self):
        time.sleep(2)
        doc = '编辑coupon template'
        #先创建模板，保证有可编辑的模板
        self.create_percent_coupon_template()
        time.sleep(2)
        # 点击编辑按钮
        self.wait_elevisble(CouponCouponTemplateLocator.edit_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.edit_template_button, doc)
        time.sleep(3)
        # 修改名称和百分比
        self.wait_elevisble(CouponCouponTemplateLocator.coupon_template_input, doc)
        old_tempalte_name = self.get_element(CouponCouponTemplateLocator.coupon_template_input, '获取原来名字').get_attribute('value')
        new_tempalte_name = old_tempalte_name + '_modify_name'
        new_percent = '50'
        self.keyboard_event(CouponCouponTemplateLocator.coupon_template_input, doc)
        self.keyboard_event(CouponCouponTemplateLocator.percent_input, doc)
        self.send_keys(CouponCouponTemplateLocator.coupon_template_input, new_tempalte_name, doc)
        self.send_keys(CouponCouponTemplateLocator.percent_input, new_percent, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        time.sleep(5)
        ######检查编辑结果
        self.wait_elevisble(CouponCouponTemplateLocator.search_input, doc)
        self.send_keys(CouponCouponTemplateLocator.search_input, new_tempalte_name, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(5)
        # 检查修改后的名称是否正确
        assert self.get_element(CouponCouponTemplateLocator.first_search_result, doc).text == new_tempalte_name
        # 检查修改后的百分比是否正确
        except_result = new_percent + '%'
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_search_result_percent, doc).text, except_result)
        # 检查模板历史是否有记录
        doc = '检查模板历史是否有记录'
        self.click_element(CouponCouponTemplateLocator.first_search_result_coupon_template, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_history_coupon_template, doc).text, old_tempalte_name)


    #复制coupon template
    def copy_coupon_template(self):
        #先创建模板，保证有可复制的模板
        self.create_fixed_amount_coupon_template()
        time.sleep(2)
        doc = '复制coupon template'
        # 点击copy按钮
        self.move_mouse_to_click_element(CouponCouponTemplateLocator.actions_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.copy_template_button, doc)
        self.click_element(CouponCouponTemplateLocator.copy_template_button, doc)
        time.sleep(5)
        copy_coupon_template = self.get_element(CouponCouponTemplateLocator.coupon_template_input, doc).get_attribute('value')
        #点击submit按钮
        self.wait_elevisble(CouponCouponTemplateLocator.submit_button, doc)
        self.click_element(CouponCouponTemplateLocator.submit_button, doc)
        time.sleep(5)
        #验证是否复制成功
        self.wait_elevisble(CouponCouponTemplateLocator.search_input, doc)
        self.send_keys(CouponCouponTemplateLocator.search_input, copy_coupon_template, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(5)
        assert self.get_element(CouponCouponTemplateLocator.first_search_result, doc).text == copy_coupon_template

    # 分配优惠券给promo crowd
    def distribute_coupon_to_promo_crowd(self, driver, cmdopt):
        #先创建模板，保证有可分配的模板
        self.create_percent_coupon_template()
        time.sleep(2)
        #创建promo crowd, 保证可以选择到promo crowd
        #点击promo crowd菜单，去创建promo crowd
        doc = '创建promo crowd, 保证可以选择到promo crowd'
        self.click_element(AdminCouponPromoCrowdLocator.promo_crowd_menu, doc)
        promo_crowd_name, account_email = AdminCouponPromoCrowdPage(driver).add_promo_crowd_member(cmdopt)

        doc = '分配coupon 给promo crowd'
        coupon_template_name = self.get_element(CouponCouponTemplateLocator.first_search_result_coupon_template, doc).text
        # 点击distribute按钮
        self.move_mouse_to_click_element(CouponCouponTemplateLocator.actions_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.distribute_button, doc)
        self.click_element(CouponCouponTemplateLocator.distribute_button, doc)
        time.sleep(2)
        # 勾选promo crowd
        self.wait_elevisble(CouponCouponTemplateLocator.distribute_to_promo_crowd, doc)
        self.click_element(CouponCouponTemplateLocator.distribute_to_promo_crowd, doc)
        time.sleep(2)
        # 输入Promo Crowd
        self.send_keys(CouponCouponTemplateLocator.promo_crowd_input, promo_crowd_name, doc)
        time.sleep(2)
        # 点击第一个选项
        self.click_element(CouponCouponTemplateLocator.first_promo_crowd_selection, doc)
        time.sleep(2)
        # 点击distribute
        self.click_element(CouponCouponTemplateLocator.distribute_button, doc)

        # 检查分配结果
        # 先查询刚分配的
        self.wait_elevisble(CouponCouponTemplateLocator.search_input, doc)
        self.send_keys(CouponCouponTemplateLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(3)
        # 进入详情页面
        self.click_element(CouponCouponTemplateLocator.first_search_result, doc)
        time.sleep(2)
        # 检查distribute history
        self.wait_elevisble(CouponCouponTemplateLocator.distribute_history_tab, doc)
        self.click_element(CouponCouponTemplateLocator.distribute_history_tab, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_distribute_history_account_email, doc).text, account_email)
        time.sleep(3)
        # 到coupon records页面检查分配结果
        doc = '到coupon records页面检查分配结果'
        self.click_element(AdminCouponCouponRecordsLocator.coupon_records_menu, doc)
        time.sleep(5)
        self.wait_elevisble(AdminCouponCouponRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponCouponRecordsLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        self.click_element(AdminCouponCouponRecordsLocator.search_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminCouponCouponRecordsLocator.first_search_result_account_email, doc).text, account_email)
        # 将coupon no return，给其他case
        coupon_no = self.get_element(AdminCouponCouponRecordsLocator.first_search_result, doc).text
        return coupon_no



    # 分配优惠券给个人
    def distribute_coupon_to_personal(self, cmdopt):
        # 先创建模板，保证有可分配的模板
        self.create_percent_coupon_template()
        time.sleep(2)
        doc = '分配优惠券给个人'
        coupon_template_name = self.get_element(CouponCouponTemplateLocator.first_search_result_coupon_template, doc).text
        # 点击distribute按钮
        self.move_mouse_to_click_element(CouponCouponTemplateLocator.actions_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.distribute_button, doc)
        self.click_element(CouponCouponTemplateLocator.distribute_button, doc)
        time.sleep(2)
        # 输入邮箱
        account_email = Logindata.user_data[cmdopt]['user']['username']
        self.wait_elevisble(CouponCouponTemplateLocator.account_email_input, doc)
        self.send_keys(CouponCouponTemplateLocator.account_email_input, account_email, doc)
        time.sleep(2)
        # 点击搜索到的邮箱
        self.click_element(CouponCouponTemplateLocator.first_account_email_selection, doc)
        time.sleep(2)
        # 点击distribute
        self.click_element(CouponCouponTemplateLocator.distribute_button, doc)
        time.sleep(2)
        #检查分配结果
        #先查询刚分配的
        self.wait_elevisble(CouponCouponTemplateLocator.search_input, doc)
        self.send_keys(CouponCouponTemplateLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(3)
        #进入详情页面
        self.click_element(CouponCouponTemplateLocator.first_search_result, doc)
        time.sleep(2)
        #检查distribute history
        self.wait_elevisble(CouponCouponTemplateLocator.distribute_history_tab, doc)
        self.click_element(CouponCouponTemplateLocator.distribute_history_tab, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(CouponCouponTemplateLocator.first_distribute_history_account_email, doc).text, account_email)
        time.sleep(3)
        #到coupon records页面检查分配结果
        doc = '到coupon records页面检查分配结果'
        self.click_element(AdminCouponCouponRecordsLocator.coupon_records_menu, doc)
        time.sleep(5)
        self.wait_elevisble(AdminCouponCouponRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponCouponRecordsLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        self.click_element(AdminCouponCouponRecordsLocator.search_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminCouponCouponRecordsLocator.first_search_result_account_email, doc).text, account_email)
        #将coupon no return，给其他case
        coupon_no = self.get_element(AdminCouponCouponRecordsLocator.first_search_result, doc).text
        return coupon_no


    # disable coupon template
    def disable_coupon_template(self):
        # 先创建模板，保证有可disabled的模板
        self.create_fixed_amount_coupon_template()
        time.sleep(2)
        doc = 'disable coupon template'
        self.move_mouse_to_click_element(CouponCouponTemplateLocator.actions_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.disable_button, doc)
        self.click_element(CouponCouponTemplateLocator.disable_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.disable_ok_button, doc)
        self.click_element(CouponCouponTemplateLocator.disable_ok_button, doc)
        time.sleep(3)
        #校验是否disabled
        self.wait_elevisble(CouponCouponTemplateLocator.actions_button, doc)
        self.move_mouse_to_click_element(CouponCouponTemplateLocator.actions_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.enable_button, doc)
        #判断enable 按钮的是否亮显
        result = self.get_element(CouponCouponTemplateLocator.enable_button_high_light, doc).get_attribute('aria-disabled')
        assert result == 'false'

    # enable coupon template
    def enable_coupon_template(self):
        # 先创建模板,并将模板disabled，保证有可enabled的模板
        self.disable_coupon_template()
        time.sleep(2)
        doc = 'enable coupon template'
        self.wait_elevisble(CouponCouponTemplateLocator.enable_button, doc)
        self.click_element(CouponCouponTemplateLocator.enable_button, doc)
        time.sleep(2)
        #校验是否enable成功
        self.wait_elevisble(CouponCouponTemplateLocator.actions_button, doc)
        self.move_mouse_to_click_element(CouponCouponTemplateLocator.actions_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.enable_button, doc)
        #判断enable 按钮的是否灰显
        result = self.get_element(CouponCouponTemplateLocator.enable_button_not_high_light, doc).get_attribute('aria-disabled')
        assert result == 'true'

    # generate coupon
    def generate_coupon(self):
        # 先创建模板,先创建模板，保证有可generate的数据
        self.create_percent_coupon_template()
        time.sleep(2)
        doc = 'generate coupon'
        # 获取coupon template的名称
        coupon_template_name = self.get_element(CouponCouponTemplateLocator.search_input, doc).get_attribute('value')

        self.move_mouse_to_click_element(CouponCouponTemplateLocator.actions_button, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.generate_button, doc)
        self.click_element(CouponCouponTemplateLocator.generate_button, doc)
        time.sleep(2)
        #输入数量
        coupon_number = '11'
        self.wait_elevisble(CouponCouponTemplateLocator.quantity_input, doc)
        self.send_keys(CouponCouponTemplateLocator.quantity_input, coupon_number, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.generate_button, doc)
        time.sleep(3)
        #检查generate结果
        #查询刚分配的coupon template
        doc = '检查generate结果'
        self.send_keys(CouponCouponTemplateLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.search_button, doc)
        time.sleep(5)
        #点击详情
        self.click_element(CouponCouponTemplateLocator.first_search_result, doc)
        self.wait_elevisble(CouponCouponTemplateLocator.generate_history_tab, doc)
        self.click_element(CouponCouponTemplateLocator.generate_history_tab, doc)
        #滚动到底下，校验的返回的数据量是否和创建的一致
        js_down = "window.scrollTo(0,1000)"
        self.excute_js(js_down)
        time.sleep(2)
        search_num = self.get_element(CouponCouponTemplateLocator.search_result_num, doc).text
        assert coupon_number in search_num, '实际值:' + search_num + '期望值:' + coupon_number

        #检验Coupon Records 页面有优惠券记录
        doc = '检验Coupon Records 页面有优惠券记录'
        #访问Coupon Records页面
        self.click_element(AdminCouponCouponRecordsLocator.coupon_records_menu, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponCouponRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponCouponRecordsLocator.search_input, coupon_template_name, doc)
        time.sleep(2)
        #点击搜索
        self.click_element(AdminCouponCouponRecordsLocator.search_button, doc)
        time.sleep(2)
        #校验的返回的数据量是否和创建的一致
        doc = '校验的返回的数据量是否和创建的一致'
        self.wait_elevisble(AdminCouponCouponRecordsLocator.search_result_num, doc)
        search_num = self.get_element(AdminCouponCouponRecordsLocator.search_result_num, doc).text
        assert coupon_number in search_num, '实际值:' + search_num + '期望值:' + coupon_number
        # 返回优惠券的码，给其他case用
        doc = '返回优惠券的码，给其他case用'
        return self.get_element(AdminCouponCouponRecordsLocator.first_search_result, doc).text, coupon_template_name


    # 筛选 coupon template
    def filter_coupon_template(self):
        # 先创建模板,并将模板disabled，保证有可enabled的模板
        self.disable_coupon_template()
        time.sleep(2)
        doc = '筛选 coupon template'
        #筛选enabled的
        self.click_element(CouponCouponTemplateLocator.status_filter_button, doc)
        time.sleep(2)
        # self.wait_elevisble(CouponCouponTemplateLocator.status_filter_enabled_button, doc)
        self.click_element(CouponCouponTemplateLocator.status_filter_enabled_button, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.status_filter_ok_button, doc)
        time.sleep(5)
        #检查筛选结果, coupon template是disabled状态，没有筛选结果
        result = self.element_is_exist(CouponCouponTemplateLocator.first_search_result)
        assert not result

        #筛选enabled的
        doc = '筛选 disabled coupon template'
        self.click_element(CouponCouponTemplateLocator.status_filter_button, doc)
        #取消勾选enabled
        time.sleep(2)
        # self.wait_elevisble(CouponCouponTemplateLocator.status_filter_enabled_button, doc)
        self.click_element(CouponCouponTemplateLocator.status_filter_enabled_button, doc)
        time.sleep(2)
        #勾选disabled
        self.click_element(CouponCouponTemplateLocator.status_filter_disabled_button, doc)
        time.sleep(2)
        self.click_element(CouponCouponTemplateLocator.status_filter_ok_button, doc)
        time.sleep(5)
        #检查筛选结果, coupon template是disabled状态，有筛选结果
        result = self.element_is_exist(CouponCouponTemplateLocator.first_search_result)
        assert result

    #校验User 端分配结果
    def check_user_distribute_result(self, coupon_no):
        doc = '校验User 端分配结果'
        self.wait_elevisble(CouponMyCouponLocator.my_coupon_menu, doc)
        self.click_element(CouponMyCouponLocator.my_coupon_menu, doc)
        time.sleep(3)
        self.wait_elevisble(CouponMyCouponLocator.search_input, doc)
        self.send_keys(CouponMyCouponLocator.search_input, coupon_no, doc)
        time.sleep(2)
        self.click_element(CouponMyCouponLocator.search_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(CouponMyCouponLocator.first_search_result, doc).text, coupon_no)





    # Search = driver.find_elements_by_xpath("//*[contains(text(), 'instagram.com')]")
    # if len(search) > 0:
    #     print("Found what I was looking for")









































